const http = require('../../utils/http.js');   //相对路径
Page({
  /**
   * 页面的初始数据
   */
  data: {
    select:true,
    number:'陕AW3455',
    userInfo:''
  },
  takePhoto() {
    const ctx = wx.createCameraContext()
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        this.imageBs(res.tempImagePath)
      }
    })
  },
  error(e) {
    console.log(e.detail)
  },
  selsct(){
    this.setData({
      select: true
    })
  },
  selsct1(){
    this.setData({
      select: false
    })
  },
  dian(){
    this.takePhoto()
  },
  // 重新扫描
  cancel(){
    this.takePhoto()
  },
  imageBs(photo){
    let _this = this;
    wx.getImageInfo({
      src: photo,
      success: function (res) {
        var ctx = wx.createCanvasContext('photo_canvas');
        //设置canvas尺寸
        var towidth = 344;           //按宽度344px的比例压缩
        var toheight = Math.trunc(344*res.height/res.width);        //根据图片比例换算出图片高度
        ctx.drawImage(photo, 0, 0, towidth, toheight)
        ctx.draw(true, setTimeout(()=> {
          wx.canvasToTempFilePath({
            canvasId: 'photo_canvas',
            fileType:"jpg",
            success: function (res) {
              _this.urlTobase64(photo);
            },
            fail:(error)=>{
              wx.showToast({
                title: 'error',
                icon: 'error'
              })
            }
          })
        },300))
      }
    })
  },
  // 图片转化为base64格式
  urlTobase64(url){
    wx.getFileSystemManager().readFile({
      filePath: url, 
      encoding: "base64",
      success: res => { 
        http.postRequest('/mini/v1/ocr/license_plate_work',{image: 'data:image/png;base64,' + res.data}, res=> {
          console.log(res);
          if(res.code == 1) {
            console.log(1);
            wx.navigateTo({
              url: '/pages/classify/child/newcar?carpai=' + res.data.car_prefix + '&carnum='+ res.data.car_number 
            })
          }else if(res.code == 0) {
            console.log(2)
            wx.navigateTo({
              url: '/pages/openOrder/repair/repair?carnum='+ res.data.car_prefix + res.data.car_number
            })
          } 
        })
      }
    })
  }
})